package jaux.tank.utils;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import jaux.tank.utils.factory.Page;

public class PageUtils {

	@SuppressWarnings("deprecation")
	public static Pageable convertPageToPageable(Page<?> page) {
		Pageable pageable = null;
        if(page.isOpenSort() && page.getOrderByField()!=null) {
             pageable = PageRequest.of(page.getCurrent()-1
            		 , page.getSize()
            		 , (page.isAsc() ? Sort.Direction.ASC : Sort.Direction.DESC)
            		 , page.getOrderByField().split(","));
        }else{
            pageable = PageRequest.of(page.getCurrent()-1,page.getSize(),Sort.Direction.DESC,"id");
        }
        return pageable;
	}
}
